package com.ld.oauth2.utils;



import org.apache.commons.lang3.StringUtils;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.TimeZone;

public class DateUtil {
	public static final String C_DATE_DIVISION = "-";

	public static final String C_TIME_PATTON_DEFAULT = "yyyy-MM-dd HH:mm:ss";
	public static final String C_DATE_PATTON_DEFAULT = "yyyy-MM-dd";
	public static final String C_DATA_PATTON_YYYYMMDD = "yyyyMMdd";
	public static final String C_TIME_PATTON_HHMMSS = "HH:mm:ss";
	public static final String C_DATE_PATTON_YYYYMMDDHHMMSS = "yyyyMMddHHmmss";
	public static final String C_DATE_PATTON_DDMMYYYY = "dd/MM/yyyy";

	public static final int C_ONE_SECOND = 1000;
	public static final int C_ONE_MINUTE = 60 * C_ONE_SECOND;
	public static final int C_ONE_HOUR = 60 * C_ONE_MINUTE;
	public static final long C_ONE_DAY = 24 * C_ONE_HOUR;
	public static final String CommonPatterns[] = new String[] { C_TIME_PATTON_DEFAULT, C_DATE_PATTON_DEFAULT, C_DATA_PATTON_YYYYMMDD, C_TIME_PATTON_HHMMSS,
			C_DATE_PATTON_YYYYMMDDHHMMSS };

	/**
	 * Return the current date
	 * 
	 * @return － DATE<br>
	 */
	public static Date getCurrentDate() {
		Calendar cal = Calendar.getInstance();
		Date currDate = cal.getTime();
		return currDate;
	}

	/**
	 * 将20090510102050转成Date类型 对应日期2009-05-10 10:20:50
	 * 
	 * @param dateStr
	 * @return
	 */
	public static Date parseBossDate(String dateStr) {
		SimpleDateFormat formate = new SimpleDateFormat(DateUtil.C_DATE_PATTON_YYYYMMDDHHMMSS);
		try {
			return formate.parse(dateStr);
		} catch (ParseException e) {
			return null;
		}
	}

	/**
	 * Return the current date string
	 * 
	 * @return － 产生的日期字符串<br>
	 */
	public static String getCurrentDateStr() {
		Calendar cal = Calendar.getInstance();
		Date currDate = cal.getTime();

		return format(currDate);
	}

	/**
	 * Return the current date in the specified format
	 * 
	 * @param strFormat
	 * @return
	 */
	public static String getCurrentDateStr(String strFormat) {
		Calendar cal = Calendar.getInstance();
		Date currDate = cal.getTime();
		return format(currDate, strFormat);
	}

	/**
	 * Parse a string and return a date value
	 * 
	 * @param dateValue
	 * @return
	 * @throws Exception
	 */
	public static Date parseDate(String dateValue) {
		return parseDate(C_DATE_PATTON_DEFAULT, dateValue);
	}

	/**
	 * Parse a strign and return a datetime value
	 * 
	 * @param dateValue
	 * @return
	 */
	public static Date parseDateTime(String dateValue) {
		return parseDate(C_TIME_PATTON_DEFAULT, dateValue);
	}



	/**
	 * Parse a string and return the date value in the specified format
	 * 
	 * @param strFormat
	 * @param dateValue
	 * @return
	 * @throws ParseException
	 * @throws Exception
	 */
	public static Date parseDate(String strFormat, String dateValue) {

		if (StringUtils.isEmpty(dateValue)) {
			return null;
		}
		if (null == strFormat) {
			strFormat = C_TIME_PATTON_DEFAULT;
		}

		SimpleDateFormat dateFormat = new SimpleDateFormat(strFormat);
		dateFormat.setTimeZone(TimeZone.getTimeZone("GMT+8"));
		Date newDate = null;
		try {
			newDate = dateFormat.parse(dateValue);
		} catch (ParseException pe) {
			// loggerService.error("", pe);
			newDate = null;
		}

		return newDate;
	}

	/**
	 * 将Timestamp类型的日期转换为系统参数定义的格式的字符串。
	 * 
	 * @param aTs_Datetime
	 *            需要转换的日期。
	 * @return 转换后符合给定格式的日期字符串
	 */
	public static String format(Date aTs_Datetime) {
		return format(aTs_Datetime, C_DATE_PATTON_DEFAULT);
	}

	/**
	 * 将Timestamp类型的日期转换为系统参数定义的格式的字符串。
	 * 
	 * @param aTs_Datetime
	 *            需要转换的日期。
	 * @return 转换后符合给定格式的日期字符串
	 */
	public static String formatTime(Date aTs_Datetime) {
		return format(aTs_Datetime, C_TIME_PATTON_DEFAULT);
	}

	/**
	 * 将Date类型的日期转换为系统参数定义的格式的字符串。
	 * 
	 * @param aTs_Datetime
	 * @param as_Pattern
	 * @return
	 */
	public static String format(Date aTs_Datetime, String as_Pattern) {
		if (aTs_Datetime == null || as_Pattern == null) {
			return null;
		}
		SimpleDateFormat dateFromat = new SimpleDateFormat();
		dateFromat.applyPattern(as_Pattern);

		return dateFromat.format(aTs_Datetime);
	}

	/**
	 * @param aTs_Datetime
	 * @param as_Format
	 * @return
	 */
	public static String formatTime(Date aTs_Datetime, String as_Format) {
		if (aTs_Datetime == null || as_Format == null)
			return null;

		SimpleDateFormat dateFromat = new SimpleDateFormat();
		dateFromat.applyPattern(as_Format);

		return dateFromat.format(aTs_Datetime);
	}

	public static String getFormatTime(Date dateTime) {
		return formatTime(dateTime, C_TIME_PATTON_HHMMSS);
	}

	/**
	 * @param aTs_Datetime
	 * @param as_Pattern
	 * @return
	 */
	public static String format(Timestamp aTs_Datetime, String as_Pattern) {
		if (aTs_Datetime == null || as_Pattern == null)
			return null;

		SimpleDateFormat dateFromat = new SimpleDateFormat();
		dateFromat.applyPattern(as_Pattern);

		return dateFromat.format(aTs_Datetime);
	}

	/**
	 * 取得当前日期N天前的日期
	 * 
	 * @param date
	 * @param days
	 * @return
	 */
	public static Date dynamicDays(String days) {
		SimpleDateFormat dateFromat = new SimpleDateFormat("yyyy-MM-dd");
		Date retDate = null;
		try {
			Calendar cal = Calendar.getInstance();
			int date_int = (Integer.parseInt(days));
			if (date_int <= 0)
				return retDate;
			date_int = (date_int * -1) + 1;
			cal.add(Calendar.DAY_OF_YEAR, date_int);
			retDate = dateFromat.parse(dateFromat.format(cal.getTime()));
		} catch (Exception e) {
			e.printStackTrace();
		}
		return retDate;
	}

	/**
	 * 计算两个日期之间相差的天数
	 * 
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static int daysBetween(Date date1, Date date2) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date1);
		long time1 = cal.getTimeInMillis();
		cal.setTime(date2);
		long time2 = cal.getTimeInMillis();
		long between_days = (time2 - time1) / (1000 * 3600 * 24);

		return Integer.parseInt(String.valueOf(between_days));
	}

	/**
	 * 计算两个日期之间相差的时间戳
	 * 
	 * @param date1
	 * @param date2
	 * @return
	 */
	public static long timeInMillisBetween(Date date1, Date date2) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date1);
		long time1 = cal.getTimeInMillis();
		cal.setTime(date2);
		long time2 = cal.getTimeInMillis();
		long between_days = time2 - time1;
		return between_days;
	}

	/**
	 * 计算当前日期相对于"1977-12-01"的天数
	 * 
	 * @param date
	 * @return
	 */
	public static long getRelativeDays(Date date) {
		Date relativeDate = DateUtil.parseDate("yyyy-MM-dd", "1977-12-01");

		return DateUtil.daysBetween(relativeDate, date);
	}

	public static Date getDateBeforTwelveMonth() {
		String date = "";
		Calendar cla = Calendar.getInstance();
		cla.setTime(getCurrentDate());
		int year = cla.get(Calendar.YEAR) - 1;
		int month = cla.get(Calendar.MONTH) + 1;
		if (month > 9) {
			date = String.valueOf(year) + C_DATE_DIVISION + String.valueOf(month) + C_DATE_DIVISION + "01";
		} else {
			date = String.valueOf(year) + C_DATE_DIVISION + "0" + String.valueOf(month) + C_DATE_DIVISION + "01";
		}

		Date dateBefore = parseDate(date);
		return dateBefore;
	}

	/**
	 * 传入时间字符串,加一天后返回Date
	 * 
	 * @param date
	 *            时间 格式 YYYY-MM-DD
	 * @return
	 */
	public static Date addDate(String date) {
		if (date == null) {
			return null;
		}

		Date tempDate = parseDate(C_DATE_PATTON_DEFAULT, date);
		String year = format(tempDate, "yyyy");
		String month = format(tempDate, "MM");
		String day = format(tempDate, "dd");

		GregorianCalendar calendar = new GregorianCalendar(Integer.parseInt(year), Integer.parseInt(month) - 1, Integer.parseInt(day));

		calendar.add(GregorianCalendar.DATE, 1);
		return calendar.getTime();
	}

	/**
	 * 
	 * @param date
	 * @param days
	 * @return
	 */
	public static Date minusDate(String date, int days) {
		if (date == null) {
			return null;
		}
		Date tempDate = parseDate(C_DATE_PATTON_DEFAULT, date);
		String year = format(tempDate, "yyyy");
		String month = format(tempDate, "MM");
		String day = format(tempDate, "dd");
		GregorianCalendar calendar = new GregorianCalendar(Integer.parseInt(year), Integer.parseInt(month) - 1, Integer.parseInt(day) - days);
		return calendar.getTime();
	}

	/**
	 * 查询条件的开始时间
	 * 
	 * @param startDate
	 * @return
	 */
	public static Date parseBeginDate(String startDate) {
		if (null == startDate || "".equals(startDate.trim())) {
			return null;
		}
		return beginOfTheDay(parseDate(startDate));

	}

	/**
	 * 查询条件的结束时间
	 * 
	 * @param endDate
	 * @return
	 */
	public static Date parseEndDate(String endDate) {

		if (null == endDate || "".equals(endDate.trim())) {
			return null;
		}
		return endOfTheDay(parseDate(endDate));

	}

	/**
	 * 获取今天00点:00分:00秒
	 * 
	 * @return
	 */
	public static Date getTodayZero() {
		return DateUtil.parseDate(DateUtil.getCurrentDateStr("yyyy-MM-dd hh:mm:ss"));
	}

	/**
	 * 获取今天23点59分59秒
	 */
	public static Date getEndToDay() {
		Calendar calendar = Calendar.getInstance();
		calendar.set(Calendar.HOUR_OF_DAY, 23);
		calendar.set(Calendar.MINUTE, 59);
		calendar.set(Calendar.SECOND, 59);
		calendar.set(Calendar.MILLISECOND, 999);
		Date date = calendar.getTime();
		System.out.println(date);
		return date;
	}

	/**
	 * 某天的00点:00分:00秒
	 * 
	 * @param date
	 * @return java.util.Date
	 */
	public static Date beginOfTheDay(Date startTime) {
		if (startTime == null)
			return null;
		return DateUtil.parseDate(DateUtil.C_TIME_PATTON_DEFAULT, DateUtil.formatTime(startTime, DateUtil.C_TIME_PATTON_DEFAULT).substring(0, 11) + "00:00:00");
	}

	/**
	 * 某天的23点:59分:59秒
	 * 
	 * @param date
	 * @return java.util.Date
	 */
	public static Date endOfTheDay(Date endTime) {
		if (endTime == null)
			return null;
		return DateUtil.parseDate(DateUtil.C_TIME_PATTON_DEFAULT, DateUtil.formatTime(endTime, DateUtil.C_TIME_PATTON_DEFAULT).substring(0, 11) + "23:59:59");
	}

	/**
	 * 获取本月第一天的00点:00分:00秒
	 * 
	 * @return java.util.Date
	 */
	public static Date beginOfThisMonth() {
		GregorianCalendar gregorianCalendar = new GregorianCalendar();
		gregorianCalendar.get(Calendar.YEAR);
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
		String startOfMonth = gregorianCalendar.get(Calendar.YEAR) + "-" + (gregorianCalendar.get(Calendar.MONTH) + 1) + "-1";
		try {
			return simpleDateFormat.parse(startOfMonth);
		} catch (ParseException e) {
			// loggerService.error("", e);
		}
		return null;
	}

	public static Long getBetweenTime(Long startTime, Long endTime) {

		return (endTime - startTime) / 1000;

	}

	public static Date getNowDateTime(Date now) {

		String nowTimeStr = formatTime(now);

		return parseDateTime(nowTimeStr);

	}

	// 获得当前系统n月前的日期
	public static String lastNMonth(int Nmonth) {
		Date date = new Date();// 当前日期
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");// 格式化对象
		Calendar calendar = Calendar.getInstance();// 日历对象
		calendar.setTime(date);// 设置当前日期
		calendar.add(Calendar.MONTH, Nmonth);// 月份减一
		return sdf.format(calendar.getTime());
	}

	public static String dateToStr(Date date) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String str = sdf.format(date);
		return str;
	}

	public static Date AddOneDay(String date) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd");
		Date middle = parseDate("yyyy-MM-dd", date);
		String tempDate = df.format(middle.getTime() + 1 * 24 * 60 * 60 * 1000);
		return parseDate("yyyy-MM-dd", tempDate);
	}

	public static Date Add8hoursDay(String date) {
		SimpleDateFormat df = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date middle = parseDate("yyyy-MM-dd HH:mm:ss", date);
		String tempDate = df.format(middle.getTime() + 8 * 60 * 60 * 1000);
		return parseDate("yyyy-MM-dd", tempDate);
	}




	/**
	 * 当前日期之前的n天日期 n为整数
	 * 
	 * @param date
	 * @return
	 */
	public static Date AddDay(int n) {
		long nowTime = System.currentTimeMillis();
		return new Date(nowTime - n * 24 * 60 * 60 * 1000);
	}

	public static Date AddOneDay(String format, String date) {
		SimpleDateFormat df = new SimpleDateFormat(format);
		return DateUtil.parseDate(format, df.format(DateUtil.parseDate(format, date).getTime() + 1 * 24 * 60 * 60 * 1000));
	}

	public static String getXmlTimestamp() {
		SimpleDateFormat sdf = new SimpleDateFormat();
		sdf.applyPattern("yyyyMMddHHmmssSSSS");
		return sdf.format(new Date());

	}

	/**
	 * 获得当月的第一天, 如2009-10-01 00:00:00
	 * 
	 * @param date
	 * @return
	 */
	public static Date getFirstDayOfMonth(Date date) {
		GregorianCalendar gc = new GregorianCalendar();
		gc.setTime(date);
		gc.set(Calendar.DAY_OF_MONTH, gc.getActualMinimum(Calendar.DAY_OF_MONTH));
		gc.set(Calendar.HOUR_OF_DAY, gc.getActualMinimum(Calendar.HOUR_OF_DAY));
		gc.set(Calendar.MINUTE, gc.getActualMinimum(Calendar.MINUTE));
		gc.set(Calendar.SECOND, gc.getActualMinimum(Calendar.SECOND));
		return gc.getTime();
	}

	/**
	 * 获得当月的最后一天, 如2009-10-30 23:59:59
	 * 
	 * @param date
	 * @return
	 */
	public static Date getLastDayOfMonth(Date date) {
		GregorianCalendar gc = new GregorianCalendar();
		gc.setTime(date);
		gc.set(Calendar.DAY_OF_MONTH, gc.getActualMaximum(Calendar.DAY_OF_MONTH));
		gc.set(Calendar.HOUR_OF_DAY, gc.getActualMaximum(Calendar.HOUR_OF_DAY));
		gc.set(Calendar.MINUTE, gc.getActualMaximum(Calendar.MINUTE));
		gc.set(Calendar.SECOND, gc.getActualMaximum(Calendar.SECOND));
		return gc.getTime();
	}

	/**
	 * 获取某周第一天日期 00点:00分:00秒
	 * 
	 * @param date
	 *            这周中的一天
	 * @param start
	 *            1--第一天是周日， 2--第一天是周一， 类推
	 * @return
	 */
	public static Date getFirstDayOfWeek(Date date, int start) {
		GregorianCalendar cal = new GregorianCalendar();
		cal.setTime(date);
		int day_of_week = cal.get(Calendar.DAY_OF_WEEK) - start;
		cal.add(Calendar.DATE, -day_of_week);

		cal.set(Calendar.HOUR_OF_DAY, cal.getActualMinimum(Calendar.HOUR_OF_DAY));
		cal.set(Calendar.MINUTE, cal.getActualMinimum(Calendar.MINUTE));
		cal.set(Calendar.SECOND, cal.getActualMinimum(Calendar.SECOND));
		return cal.getTime();
	}

	// 上月第一天

	public static String getPreviousMonthFirst() {
		String str = "";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar lastDate = Calendar.getInstance();
		lastDate.set(Calendar.DATE, 1);// 设为当前月的1号
		lastDate.add(Calendar.MONTH, -1);// 减一个月，变为下月的1号
		// lastDate.add(Calendar.DATE,-1);//减去一天，变为当月最后一天
		str = sdf.format(lastDate.getTime());
		return str;
	}

	/**
	 * 获取某周最后一天日期 23:59:59
	 * 
	 * @param date
	 *            这周中的一天
	 * @param start
	 *            start 1--第一天是周日， 2--第一天是周一， 类推
	 * @return
	 */
	public static Date getLastDayOfWeek(Date date, int start) {
		GregorianCalendar cal = new GregorianCalendar();
		cal.setTime(date);
		int day_of_week = cal.get(Calendar.DAY_OF_WEEK) - start;
		cal.add(Calendar.DATE, -day_of_week); // 获得第一天
		cal.add(Calendar.DATE, 6); // +6天 就得到最后一天

		cal.set(Calendar.HOUR_OF_DAY, cal.getActualMaximum(Calendar.HOUR_OF_DAY));
		cal.set(Calendar.MINUTE, cal.getActualMaximum(Calendar.MINUTE));
		cal.set(Calendar.SECOND, cal.getActualMaximum(Calendar.SECOND));
		return cal.getTime();
	}

	// 获得上月最后一天的日期
	public static String getPreviousMonthEnd() {
		String str = "";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar lastDate = Calendar.getInstance();
		lastDate.add(Calendar.MONTH, -1);// 减一个月
		lastDate.set(Calendar.DATE, 1);// 把日期设置为当月第一天
		lastDate.roll(Calendar.DATE, -1);// 日期回滚一天，也就是本月最后一天
		str = sdf.format(lastDate.getTime());
		return str;
	}

	// 获得下个月第一天的日期

	public static String getNextMonthFirst() {
		String str = "";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar lastDate = Calendar.getInstance();
		lastDate.add(Calendar.MONTH, 1);// 减一个月
		lastDate.set(Calendar.DATE, 1);// 把日期设置为当月第一天
		str = sdf.format(lastDate.getTime());
		return str;
	}

	// 获得下个月最后一天的日期

	public static String getNextMonthEnd() {
		String str = "";
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		Calendar lastDate = Calendar.getInstance();
		lastDate.add(Calendar.MONTH, 1);// 加一个月
		lastDate.set(Calendar.DATE, 1);// 把日期设置为当月第一天
		lastDate.roll(Calendar.DATE, -1);// 日期回滚一天，也就是本月最后一天
		str = sdf.format(lastDate.getTime());
		return str;
	}

	/**
	 * 获取某个日期mins分钟后的日期
	 * 
	 * @param date
	 * @param mins
	 * @return
	 */
	public static Date getAfterNMinute(Date date, int mins) {
		GregorianCalendar cal = new GregorianCalendar();
		cal.setTime(date);
		cal.add(Calendar.MINUTE, mins);
		return cal.getTime();

	}

	/**
	 * 从年，月，日，时，分，秒来判断两个日期是否相等，转成字符串后进行比较
	 * 
	 * @param d1
	 * @param d2
	 * @return
	 */
	public static Boolean compareDate(Date d1, Date d2) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		String s1 = sdf.format(d1);
		String s2 = sdf.format(d2);
		return s1.equals(s2);
	}

	/**
	 * 判断date是否在d1与d2之间
	 * 
	 * @param date
	 * @param d1
	 * @param d2
	 * @return
	 */
	public static Boolean DateIfBetweenDates(Date date, Date d1, Date d2) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		long dateTime = cal.getTimeInMillis();
		cal.setTime(d1);
		long time1 = cal.getTimeInMillis();
		cal.setTime(d2);
		long time2 = cal.getTimeInMillis();

		long start = dateTime - time1;
		long end = dateTime - time2;
		return (start >= 0 && end <= 0);
	}

	/**
	 * 从年，月，日来判断两个日期是否相等，转成字符串后进行比较
	 * 
	 * @param d1
	 * @param d2
	 * @return
	 */
	public static Boolean compareDate2(Date d1, Date d2) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		String s1 = sdf.format(d1);
		String s2 = sdf.format(d2);
		return s1.equals(s2);
	}

	/**
	 * 增加对通用格式的识别。
	 * 
	 * @param dateStr
	 * @return
	 */
	public static Date parseDateCommon(String dateStr) {
		if (dateStr == null) {
			return null;
		}
		try {
			for (int i = 0; i < CommonPatterns.length; i++) {
				if (dateStr.length() == CommonPatterns[i].length()) {
					return new SimpleDateFormat(CommonPatterns[i]).parse(dateStr);
				}
			}
		} catch (Exception e) {
		}
		return null;
	}

	/**
	 * 取日期的相差n月的第一天
	 * 
	 * @param now
	 * @param n
	 * @return
	 */
	public static Date getPreviousMonth(Date now, int n) {
		Calendar lastDate = Calendar.getInstance();
		lastDate.setTime(now);
		lastDate.add(Calendar.MONTH, n);// 减一个月
		lastDate.set(Calendar.DATE, 1);
		return lastDate.getTime();
	}

	/**
	 * 取日期的相差n周的第一天
	 * 
	 * @param now
	 * @param n
	 * @return
	 */
	public static Date getFirstDayOfWeekMonth(Date now, int n) {
		Calendar lastDate = Calendar.getInstance();
		lastDate.setTime(now);
		lastDate.add(Calendar.WEEK_OF_MONTH, n);// 减一个月
		lastDate.set(Calendar.DAY_OF_WEEK, 2);
		return lastDate.getTime();
	}

	public static String dateStringFormat(Date d) {
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmmssSSS");
		return format.format(d);
	}

	public static String dateStringFormat2(Date d) {
		SimpleDateFormat format = new SimpleDateFormat("yyyyMMddHHmm");
		return format.format(d);
	}

	public static String dateStringFormat3(Date d) {
		SimpleDateFormat format = new SimpleDateFormat("HHmmssSSS");
		return format.format(d);
	}

	public static String getCurrentDate(String dateFormat) {
		return new SimpleDateFormat(dateFormat).format(new Date());
	}

	/**
	 * 按日加减日期
	 * 
	 * @param date
	 *            ：日期
	 * @param num
	 *            ：要加减的日数
	 * @return：成功，则返回加减后的日期；失败，则返回null
	 */
	public static Date addDays(Date date, int num) {
		if (date == null) {
			return null;
		}
		Calendar c = Calendar.getInstance();
		c.setTime(date);
		c.add(5, num);

		return c.getTime();
	}

	/**
	 * 取出一个指定长度大小的随机正整数.
	 * 
	 * @param length
	 *            int 设定所取出随机数的长度。length小于11
	 * @return int 返回生成的随机数。
	 */
	public static int getRandom(int length) {
		int num = 1;
		double random = Math.random();
		if (random < 0.1D) {
			random += 0.1D;
		}
		for (int i = 0; i < length; ++i) {
			num *= 10;
		}
		return (int) (random * num);
	}

	/**
	 * @return
	 * @功能描述：生成msgId
	 */
	public static String getMsgId() {
		int ran = getRandom(10);
		String msgId = getCurrentDate(C_DATE_PATTON_YYYYMMDDHHMMSS) + "-" + String.valueOf(ran);
		return msgId;
	}

	/**
	 * 当前日期的前一天
	 */
	public static Date getNextDay() {
		Calendar calendar = Calendar.getInstance();// 此时打印它获取的是系统当前时间
		calendar.add(Calendar.DATE, -1); // 得到前一天
		Date yestedayDate = calendar.getTime();
		return yestedayDate;
	}

	/**
	 * 当前日期的前几天
	 */
	public static Date getNewDay(Integer num) {
		Calendar calendar = Calendar.getInstance();// 此时打印它获取的是系统当前时间
		calendar.add(Calendar.DATE, -num); // 得到前一天
		Date newDate = calendar.getTime();
		return newDate;
	}




	/**
	 * 获取指定日期的指定天的时间
	 * 
	 * @param date
	 * @param d
	 * @return
	 */
	public static Date getDayByDate(Date date, int d) {
		if (date == null) {
			return null;
		}

		String year = format(date, "yyyy");
		String month = format(date, "MM");

		GregorianCalendar calendar = new GregorianCalendar(Integer.parseInt(year), Integer.parseInt(month) - 1, d);

		return calendar.getTime();
	}

	/**
	 * 获取指定日期的N月的日期
	 * 
	 * @param date
	 * @param d
	 * @return
	 */
	public static Date getMonthDateByDate(Date date, int n) {
		if (date == null) {
			return null;
		}

		String year = format(date, "yyyy");
		String month = format(date, "MM");
		String day = format(date, "dd");
		String hh = format(date, "HH");
		String mm = format(date, "mm");
		String ss = format(date, "ss");

		GregorianCalendar calendar = new GregorianCalendar(Integer.parseInt(year), Integer.parseInt(month) - 1, Integer.parseInt(day), Integer.parseInt(hh),
				Integer.parseInt(mm), Integer.parseInt(ss));
		calendar.add(Calendar.MONTH, n);
		return calendar.getTime();
	}

	public static Date getMonthDateByDateNew(Date date) {
		if (date == null) {
			return null;
		}

		String year = format(date, "yyyy");
		String month = format(date, "MM");
		String day = format(date, "dd");
		String hh = format(date, "HH");
		String mm = format(date, "mm");
		String ss = format(date, "ss");

		GregorianCalendar calendar = new GregorianCalendar(Integer.parseInt(year), Integer.parseInt(month)-1, Integer.parseInt(day), Integer.parseInt(hh),
				Integer.parseInt(mm), Integer.parseInt(ss));
		//calendar.add(Calendar.MONTH, Integer.parseInt(month));
		return calendar.getTime();
	}
	/**
	 *
	 * <p>Description: 本地时间转化为UTC时间</p>
	 * @param localTime
	 * @return
	 * @author wgs
	 * @date  2018年10月19日 下午2:23:43
	 *
	 */
	public static Date localToUTC(String localTime) {
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date localDate= null;
		try {
			localDate = sdf.parse(localTime);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		long localTimeInMillis=localDate.getTime();
		/** long时间转换成Calendar */
		Calendar calendar= Calendar.getInstance();
		calendar.setTimeInMillis(localTimeInMillis);
		/** 取得时间偏移量 */
		int zoneOffset = calendar.get(Calendar.ZONE_OFFSET);
		/** 取得夏令时差 */
		int dstOffset = calendar.get(Calendar.DST_OFFSET);
		/** 从本地时间里扣除这些差量，即可以取得UTC时间*/
		calendar.add(Calendar.MILLISECOND, -(zoneOffset + dstOffset));
		/** 取得的时间就是UTC标准时间 */
		Date utcDate=new Date(calendar.getTimeInMillis());
		return utcDate;
	}

	/**
	 *
	 * <p>Description:UTC时间转化为本地时间 </p>
	 * @param utcTime
	 * @return
	 * @author wgs
	 * @date  2018年10月19日 下午2:23:24
	 *
	 */
	public static Date utcToLocal(String utcTime){
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		sdf.setTimeZone(TimeZone.getTimeZone("UTC"));
		Date utcDate = null;
		try {
			utcDate = sdf.parse(utcTime);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		sdf.setTimeZone(TimeZone.getDefault());
		Date locatlDate = null;
		String localTime = sdf.format(utcDate.getTime());
		try {
			locatlDate = sdf.parse(localTime);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return locatlDate;
	}

	/*
	 * 将时间转换为时间戳
	 */
	public static String dateToStamp(String s) throws ParseException{
		String res;
		SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date date = simpleDateFormat.parse(s);
		long ts = date.getTime();
		res = String.valueOf(ts);
		return res;
	}


	public static void main(String[] args) {
		Date monthDateByDateNew = getMonthDateByDateNew(new Date());
		System.out.println(monthDateByDateNew);
		Date date = new Date();
		String year = format(date, "yyyy");
		String month = format(date, "MM");
		String day = format(date, "dd");
		String hh = format(date, "HH");
		String mm = format(date, "mm");
		String ss = format(date, "ss");
		System.out.println(year);
		System.out.println(month);
		System.out.println(day);
		System.out.println(hh);
		System.out.println(mm);
		System.out.println(ss);
//		String time = "2021-07-02 23:59:59";
//	/*	System.out.println(DateUtil.utcToLocal(time));*/
//		Date date = DateUtil.localToUTC(time);
//		String format = formatTime(date);
//		System.out.printf("format============="+format);
//		String stamp = null;
//		try {
//			stamp = dateToStamp(format);
//		} catch (ParseException e) {
//			e.printStackTrace();
//		}
//		System.out.println("stamp======="+stamp);

	/*	Date ss = DateUtil.parseDate("yyyy-MM-dd HH:mm", "2016-12-30");
		System.out.println(ss.toString());

		Date startTime = DateUtil.parseDate("yyyy-MM-dd", "2014-1-1");
		// 系统当前时间
		Date nowTime = DateUtil.getCurrentDate();
		Date temp = DateUtil.getPreviousMonth(startTime, 1);
		List<String> dateList = new ArrayList<String>();
		dateList.add("2014-1-1");
		while (DateUtil.timeInMillisBetween(temp, nowTime) > 0) {
			dateList.add(DateUtil.format(temp, "yyyy-MM"));
			temp = DateUtil.getPreviousMonth(temp, 1);
		}

		for (String s : dateList) {
			System.out.println(s);
		}
*/
	}

}